package com.example.roomwordssample;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class WordListAdapter extends RecyclerView.Adapter<WordListAdapter.WordViewHolder> {

    private final LayoutInflater inflater;
    private List<Word> words;

    public WordListAdapter(Context context){
        this.inflater=LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public WordViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
        View inflate=inflater.inflate(R.layout.recyclerview_item,parent,false);
        WordViewHolder wordViewHolder=new WordViewHolder(inflate);
        return wordViewHolder;
    }

    @Override
    public void onBindViewHolder( WordListAdapter.WordViewHolder holder, int position) {
        if(words!=null){
            Word word=words.get(position);
            holder.textView.setText(word.mWord);
        }else {
            holder.textView.setText("没有单词");
        }
    }
    public void setWords(List<Word> words){
        this.words=words;
    }

    @Override
    public int getItemCount() {
        if(words!=null) {
            return words.size();
        }
        return 0;
    }

    class WordViewHolder extends RecyclerView.ViewHolder{
        private final TextView textView;
        public WordViewHolder( View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView);
        }
    }
}
